﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Data;
using Service;
using Security;
using Newtonsoft.Json;
public partial class _Default : System.Web.UI.Page
{
    #region Propiedades
    SimpleVisitante _Visitante
    {
        get
        {
            return new Seguridad().LoadCurrentUserFromMembership();
        }
    }

    #endregion
    protected void Page_PreRender(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterClientScriptInclude("Maps_Scriptage", "JavaScript/Maps_Scriptage.js");
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), "SetPosition", "window.addEvent('domready', function(){initialize('" + this._Visitante.Direccion + "','" + this._Visitante.Numero + "','" + this._Visitante.Localidad + "','" + this._Visitante.Provincia + "','" + JsonConvert.SerializeObject(this._Visitante) + "' );} );", true);
        setVistas();
        setFiltros();
    }

    #region Metodos
    void RenderMapGrid(CollectionPersona cp)
    {
        this.GvRevisitas.DataSource = cp;
        this.GvRevisitas.DataBind();
        this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), "SetPositions", "window.addEvent('domready', function(){marcarMapa('" + new VisitadorSvr().GetRevisitasInJson(cp) + "' );} );", true);
    }

    //metodos de busqueda y seteo de vistas
    void setVistas()
    {
        switch (DrpFiltros.SelectedValue.ToLower())
        {
            case "todas":
                MultiViewFiltros.SetActiveView(View1Todos);
                break;
            case "territorio":
                MultiViewFiltros.SetActiveView(View2Territorio);
                break;
            case "fecha":
                MultiViewFiltros.SetActiveView(View3FechaVisita);
                break;
        }
    }
    void setFiltros()
    {
        switch (MultiViewFiltros.ActiveViewIndex)
        {
            case 0:
                SetTodas();
                break;
            case 1:
                SetPorTerritorio();
                break;
            case 2:
                SetPorFecha();
                break;
        }

    }

    //filtros
    void SetTodas()
    {
        CollectionPersona cp = new VisitadorSvr().GetRevisitasFromVisitante(this._Visitante);
        RenderMapGrid(cp);
    }
    void SetPorTerritorio()
    {
        int territorio = Convert.ToInt32(TxtTerritorioNumero.Text);
        CollectionPersona cp = new VisitadorSvr().GetRevisitasByTerritorio(this._Visitante, territorio);
        RenderMapGrid(cp);
    }
    void SetPorFecha()
    {
        int dias = 1;
        if (ChkDias.Checked)
        {
            dias = Convert.ToInt32(TxtDias.Text);
        }
        if (ChkSemanas.Checked)
        {
            dias = 7 * Convert.ToInt32(TxtSemanas.Text);
        }

        DateTime fechaUltima = DateTime.Now.AddDays(-dias);
        CollectionPersona cp = new VisitadorSvr().GetRevisitasByFechaUltima(this._Visitante, fechaUltima);
        RenderMapGrid(cp);
    }

    #endregion
    #region Eventos
    protected void DrpFiltros_SelectedIndexChanged(object sender, EventArgs e)
    {
        //  setVistas();
        //  setFiltros();
    }
    protected void LnkNuevaRevisita_Click(object sender, EventArgs e)
    {
        Response.Redirect("NuevaRevisita.aspx");
    }
    #endregion

}
